<template>
    <div>
        <!-- 筛选条件 -->
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="网关" prop="gatewayId">
                <gateway-select @changeGateway="changeGateway"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>


        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    plain
                    :disabled="multiple"
                    @click="processHandler"
                >设为已处理
                </el-button>
                <!--                <el-button-->
                <!--                    plain-->
                <!--                    @click="processHandlerAll"-->
                <!--                >一键处理-->
                <!--                </el-button>-->
            </el-col>

            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>


        <el-row v-show="gatewayExpireList.length>0" v-loading="loading" :gutter="20"
                style="margin-top: 10px;font-size: 15px" type="flex" justify="space-around"
                v-for="(item,index) in gatewayExpireList" :key="index"
        >
            <el-col :xl="24">
                <el-card shadow="always" :class="{ 'selected': isSelected(index) }"
                         @click.native="toggleSelection(index)"
                >
                    <div slot="header" class="clearfix">
                        <span style="font-size: 14px;color: #abb2bf">{{ item.gatewayName }}</span>
                    </div>
                    <div style="display: flex;align-items: center;color: #717783">
                        <span>网关名称：</span>
                        <span>{{ item.gatewayName }}</span>
                    </div>

                    <div style="display: flex;align-items: center;margin-top: 10px;color: #717783">
                        <span>通知时间：</span>
                        <span>{{ parseTime(item.noticeTime) }}</span>
                    </div>


                    <div style="display: flex;align-items: center;margin-top: 10px;color: #717783">
                        <span>是否通知：</span>
                        <div>
                            <el-tag v-if="item.status==='0'" type="info">否</el-tag>
                            <el-tag v-if="item.status==='1'" type="success">是</el-tag>
                        </div>
                    </div>

                    <div style="display: flex;align-items: center;margin-top: 10px;color: #717783">
                        <span>到期时间：</span>
                        <span>{{ parseTime(item.expireTime) }}</span>
                    </div>
                </el-card>
            </el-col>
        </el-row>

        <el-row v-show="gatewayExpireList.length<1" :gutter="20" style="margin-top: 10px;" type="flex" justify="space-around">
            <el-col :xl="24">
                <el-empty></el-empty>
            </el-col>
        </el-row>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />
    </div>
</template>

<script>
import {delGatewayExpire, getGatewayExpire, listGatewayExpire, process} from "@/api/report/gatewayExpire"

import gatewaySelect from '@/views/common/GatewaySelect.vue'

export default {
    name: 'index',
    data() {
        return {
            showSearch: true,
            gatewayExpireList: [],
            total: 0,
            loading: false,
            multiple: true,
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                gatewayId: null,
                status: null,
                isProcess: null
            },
            ids: [],
            selectedItems: [],
            sensorList: []
        }
    },
    components: {
        gatewaySelect
    },
    created() {
        this.getList()
    },
    methods: {
        calculateIndex(index) {
            const pageNum = this.queryParams.pageNum
            const pageSize = this.queryParams.pageSize
            return index + 1 + (pageNum - 1) * pageSize
        },
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        resetQuery() {
            this.resetForm("queryForm")
            this.handleQuery()
        },
        getList() {
            this.loading = true
            listGatewayExpire(this.queryParams).then(response => {
                this.gatewayExpireList = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.multiple = !selection.length
        },
        processHandler() {
            process({ ids: this.ids }).then(response => {
                this.$message.success('成功')
                this.getList()
                this.$emit('getCount')
            })
        },
        // 接收子组件传递的消息
        changeGateway(obj) {
            this.queryParams.gatewayId = obj.gatewayId
            this.getList()
        },
        toggleSelection(itemId) {
            const index = this.selectedItems.indexOf(itemId)
            if (index === -1) {
                this.selectedItems.push(itemId)
                this.ids.push(this.gatewayExpireList[itemId].id)
            } else {
                this.selectedItems.splice(index, 1)
                this.ids.splice(index, 1)
            }
            this.multiple = !this.selectedItems.length
        },
        isSelected(index) {
            return this.selectedItems.includes(index)
        },
    }
}
</script>

<style scoped>
.selected {
    border: 1px solid red;
}
</style>
